{include file="admin@public/header" /}

<body>
    <div class="layui-fluid">
        <div class="layui-card" id="table">
            <div class="layui-card-body">
                <div>
                    <button class="layui-btn" data-type="add">添加</button>
                </div>
                <table class="layui-table" id="test" lay-filter="test"></table>
            </div>
        </div>
    </div>
    <script type="text/html" id="statusTpl">
    {{#  if(d.status == 0){ }}
    正常
    {{#  } else { }}
    禁用
    {{#  } }}
</script>
    <script type="text/html" id="optTpl">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    {{#  if(d.id != 1){ }}
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    {{#  } }}
</script>
    <script type="text/javascript">
        var curr_page = 1;
        //搜索
        function search(curr) {
            //执行重载
            table.reload('test', { page: { curr: curr ? curr : curr_page }, where: {} });
        }

        layui.use(['laydate', 'table'], function () {
            var table = layui.table;

            table.render({
                elem: '#test'
                , url: '/admin/user/lists'
                , page: true //开启分页
                , limit: 20
                , cols: [[ //表头
                    { field: 'id', title: 'ID', width: 60 }
                    , { field: 'user_name', title: '账号' }
                    , { field: 'nickname', title: '用户昵称' }
                    , { field: 'role_name', title: '所属角色' }
                    , { field: 'status', title: '状态', templet: '#statusTpl' }
                    , { field: 'create_time', title: '创建时间' }
                    , { fixed: 'right', title: '操作', toolbar: '#optTpl', width: 150 }
                ]],
                done: function (res, curr, count) {
                    curr_page = curr;
                }
            });

            $('.layui-btn').click(function () {
                var type = $(this).data('type');
                if (type == 'add') {
                    edit("{:url('/admin/user/add')}", '新增用户');
                }
            });

            //监听行工具事件
            table.on('tool(test)', function (obj) {
                var data = obj.data;
                if (obj.event === 'del') {
                    deleted("{:url('admin/user/delete')}", obj)
                } else if (obj.event === 'edit') {
                    edit("{:url('/admin/user/edit/id/" + data.id + "')}", '编辑' + data.user_name);
                }
            });
        });
    </script>
</body>

</html>